package com.wst.system.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wst.system.config.exception.GlobalExcetion;
import com.wst.system.interceptor.encrypt.EncryptDecrypt;
import com.wst.system.interceptor.encrypt.EncryptDecryptTime;
import com.wst.system.security.IsAdmin;
import com.wst.system.security.entity.*;
import com.wst.system.security.entity.dto.SysMenuEntityListDto;
import com.wst.system.security.entity.dto.SysMenuEntityTreeDto;
import com.wst.system.security.entity.dto.SysMenuEntityTreeMenuDto;
import com.wst.system.security.entity.vo.SysMenuEntityVo;
import com.wst.system.server.entity.SysConfig;
import com.wst.system.server.entity.vo.DelOneDataVo;
import com.wst.system.server.entity.vo.PageSearchVo;
import com.wst.system.server.service.*;
import com.wst.system.server.service.impl.SysUserRoleServiceImpl;
import com.wst.system.util.ResultUtil;
import com.wst.system.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @IsAdmin
    @PostMapping("/addmenu")
    public JSONObject addMenu(@RequestBody SysMenuEntityVo vo) throws GlobalExcetion {
        SysMenuEntity entity=sysMenuService.saveOrUpdate(vo);
        return ResultUtil.successInfo(entity);
    }

    @IsAdmin
    @PostMapping("/page")
    public JSONObject page(@RequestBody PageSearchVo vo) throws GlobalExcetion {
        IPage<SysMenuEntity> page= sysMenuService.page(vo.getPageNum(),vo.getPageSize(),vo.getSearch());
        return ResultUtil.successInfo(page);
    }

    @IsAdmin
    @GetMapping("/all")
    public JSONObject all() throws GlobalExcetion {
        List<SysMenuEntityListDto> list=sysMenuService.listAll();
        Integer count=list==null?0:list.size();
        return ResultUtil.successInfoTable(count,list);
    }

    @IsAdmin
    @GetMapping("/gettreelist")
    public JSONObject getTreeList() throws GlobalExcetion {
        List<SysMenuEntityTreeDto> child=sysMenuService.childList();
        return ResultUtil.successInfo(child);
    }

    @IsAdmin
    @GetMapping("/gettreesearch")
    public JSONObject getTreeSearch(@RequestParam("name")String name) throws GlobalExcetion {
       List<SysMenuEntityTreeDto> dtoList= sysMenuService.childList(name);
       return ResultUtil.successInfo(dtoList);
    }

    @Autowired
    private SysRoleMenuService sysRoleMenuService;


    @IsAdmin
    @GetMapping("/gettree2")
    public JSONObject getTree2() throws GlobalExcetion {
        List<SysRoleMenuEntity> sysRoleMenuEntityList=sysRoleMenuService.getSysRoleMenuEntityRid(4);
        List<Integer> menuIds=sysRoleMenuEntityList.stream().map(e->e.getMenuId()).collect(Collectors.toList());
        List<SysMenuEntityTreeDto> dtoList=sysMenuService.childList(menuIds);
        return ResultUtil.successInfo(dtoList);
    }

    @IsAdmin
    @PostMapping("/del")
    public JSONObject del(@RequestBody DelOneDataVo vo){
        sysMenuService.removeById(vo.getId());
        return ResultUtil.successInfo("删除成功");
    }

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService roleMenuService;

    @Autowired
    private SysConfigService sysConfigService;

    @EncryptDecryptTime
    @PostMapping("/getmenuinfo")
    public JSONObject getMenu() throws GlobalExcetion {
        //获取用户信息
        SelfUserEntity selfUser=SecurityUtil.getUserInfo();
        JSONObject jsonObject=new JSONObject();
        JSONObject homeInfo=new JSONObject();
        homeInfo.put("title","首页");
        homeInfo.put("href","page/welcome-1.html?t=1");
        jsonObject.put("homeInfo",homeInfo);
        JSONObject logoInfo=new JSONObject();
        SysConfig sysConfig=sysConfigService.getWebName();
        logoInfo.put("title",sysConfig.getSysValue());
        logoInfo.put("image","images/logo.png");
        logoInfo.put("href","");
        jsonObject.put("logoInfo",logoInfo);
        List<SysUserRoleEntity> list=sysUserRoleService.selectUserRoleEntityByUid(selfUser.getUserId());
        List<Integer> roleIds=list.stream().map(e->e.getRoleId()).collect(Collectors.toList());
        List<SysRoleMenuEntity> roleMenuEntitys=sysRoleMenuService.getByRoleIds(roleIds);
        List<SysMenuEntityTreeMenuDto> treeDtoList=new ArrayList<>();
        if(roleMenuEntitys!=null){
            List<Integer> menuIds=roleMenuEntitys.stream().map(e->e.getMenuId()).distinct().collect(Collectors.toList());
            treeDtoList=sysMenuService.childMenuListByCid(menuIds);
        }
        JSONArray menuInfo=new JSONArray();
        menuInfo.addAll(treeDtoList);
        jsonObject.put("menuInfo",menuInfo);
        return ResultUtil.successInfo(jsonObject);
    }
}
